package com.itsv.cyzx.db;

import java.util.ArrayList;

import com.itsv.cyzx.bean.WaterBean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MingXiDB extends SQLiteOpenHelper {

	SQLiteDatabase db = null;
	private static final int dbVersion = 1;
	private Context context = null;
	private static String dbName = "itsv_mingxi.db";

	public MingXiDB(Context context) {
		super(context, dbName, null, dbVersion);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

		/**
		 * 锟斤拷息锟斤拷
		 */
		String tablesql = "create table Tab_MXList(id TEXT , amount TEXT ,tradeType TEXT,"
				+ "tradeStatus TEXT,cardAsn TEXT,name TEXT,occurTime TEXT)";
		db.execSQL(tablesql);
		tablesql = "create table Tab_yue(yue TEXT)";
		db.execSQL(tablesql);
		tablesql = "create table Tab_danbi(danbi TEXT)";
		db.execSQL(tablesql);
		tablesql = "create table Tab_meitian(meitian TEXT)";
		db.execSQL(tablesql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		if (newVersion <= oldVersion) {
			return;
		} else {
			context.deleteDatabase(dbName);
			onCreate(db);
		}
	}

	/**
	 * 
	 * 
	 * @return 锟斤拷取全锟斤拷锟斤拷息锟叫憋拷锟斤拷锟�
	 */
	public String getYue() {
		String time = null;
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		String sql = "select * from Tab_yue";
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			time = cursor.getString(cursor.getColumnIndex("yue"));
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return time;
	}

	/**
	 * 锟斤拷锟斤拷锟较�
	 * 
	 * @param bean
	 */
	public void insertYue(String yue) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues cv = new ContentValues();
		cv.put("yue", yue);
		db.insert("Tab_yue", null, cv);
	}

	/**
	 * 锟斤拷息删锟斤拷
	 * 
	 * @param id
	 *            :删锟斤拷锟斤拷锟斤拷 锟斤拷锟轿猲ull 锟斤拷锟饺拷锟缴撅拷锟�
	 * @return
	 */
	public void deleteYue() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		db.delete("Tab_yue", null, null);
	}
	/**
	 * 
	 * 
	 * @return 锟斤拷取全锟斤拷锟斤拷息锟叫憋拷锟斤拷锟�
	 */
	public String getDanbi() {
		String time = null;
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		String sql = "select * from Tab_danbi";
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			time = cursor.getString(cursor.getColumnIndex("danbi"));
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return time;
	}

	/**
	 * 锟斤拷锟斤拷锟较�
	 * 
	 * @param bean
	 */
	public void insertDanbi(String danbi) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues cv = new ContentValues();
		cv.put("danbi", danbi);
		db.insert("Tab_danbi", null, cv);
	}

	/**
	 * 锟斤拷息删锟斤拷
	 * 
	 * @param id
	 *            :删锟斤拷锟斤拷锟斤拷 锟斤拷锟轿猲ull 锟斤拷锟饺拷锟缴撅拷锟�
	 * @return
	 */
	public void deleteDanbi() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		db.delete("Tab_danbi", null, null);
	}
	/**
	 * 
	 * 
	 * @return 锟斤拷取全锟斤拷锟斤拷息锟叫憋拷锟斤拷锟�
	 */
	public String getMeitian() {
		String time = null;
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		String sql = "select * from Tab_meitian";
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			time = cursor.getString(cursor.getColumnIndex("meitian"));
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return time;
	}

	/**
	 * 锟斤拷锟斤拷锟较�
	 * 
	 * @param bean
	 */
	public void insertMeitian(String meitian) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues cv = new ContentValues();
		cv.put("meitian", meitian);
		db.insert("Tab_meitian", null, cv);
	}

	/**
	 * 锟斤拷息删锟斤拷
	 * 
	 * @param id
	 *            :删锟斤拷锟斤拷锟斤拷 锟斤拷锟轿猲ull 锟斤拷锟饺拷锟缴撅拷锟�
	 * @return
	 */
	public void deleteMeitian() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		db.delete("Tab_meitian", null, null);
	}

	/**
	 * 通锟斤拷id锟斤拷询锟斤拷息
	 * 
	 * @return
	 */
	public ArrayList<WaterBean> getMXlist() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ArrayList<WaterBean> list = new ArrayList<WaterBean>();
		ArrayList<WaterBean> list2 = new ArrayList<WaterBean>();
		String sql = "select * from Tab_MXList ";
		Cursor cursor = db.rawQuery(sql, null);
		WaterBean bean = null;
		while (cursor.moveToNext()) {
			bean = new WaterBean();
			bean.setId(cursor.getString(cursor.getColumnIndex("id")));
			bean.setAmount(cursor.getString(cursor.getColumnIndex("amount")));
			bean.setTradeType(cursor.getString(cursor
					.getColumnIndex("tradeType")));
			bean.setTradeStatus(cursor.getString(cursor
					.getColumnIndex("tradeStatus")));
			bean.setCardAsn(cursor.getString(cursor.getColumnIndex("cardAsn")));
			bean.setName(cursor.getString(cursor.getColumnIndex("name")));
			bean.setOccurTime(cursor.getString(cursor
					.getColumnIndex("occurTime")));
			list2.add(bean);
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		if (list2 != null && list2.size() > 0) {
			for (int i = list2.size() - 1; i >= 0; i--) {
				list.add(list2.get(i));
			}
			return list;
		} else {

			return list2;
		}
	}

	/**
	 * 锟斤拷锟斤拷锟较?
	 * 
	 * @param bean
	 */
	public void insertMXList(WaterBean bean) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues cv = new ContentValues();
		cv.put("id", bean.getId());
		cv.put("amount", bean.getAmount());
		cv.put("tradeType", bean.getTradeType());
		cv.put("tradeStatus", bean.getTradeStatus());
		cv.put("cardAsn", bean.getCardAsn());
		cv.put("name", bean.getName());
		cv.put("occurTime", bean.getOccurTime());
		db.insert("Tab_MXList", null, cv);
	}

	/**
	 * 锟斤拷息删锟斤拷
	 * 
	 * @param id
	 *            :删锟斤拷锟斤拷锟斤拷 锟斤拷锟轿猲ull 锟斤拷锟饺拷锟缴撅拷锟?
	 * @return
	 */
	public void deleteMXListData() {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		db.delete("Tab_MXList", null, null);
	}

	/**
	 * 锟截憋拷锟斤拷菘锟?
	 */
	public void closeDB() {
		if (db != null) {
			db.close();
			db = null;
		}
	}
}
